package com.study.daily.algozuo.y2021.m10.day1020;

/**
 * @descriptions: SelectionSort
 * @author: Tom
 * @date: 2021/10/20 下午 02:54
 * @version: 1.0
 */
public class Code01_SelectionSort {
    public static void main(String[] args) {
        int[] nums = new int[]{2, 3, 7, 8, 2, 1, 10};
        insertSort(nums);
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }

    public static void insertSort(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i; j >= 0 && nums[j] > nums[j + 1]; j--) {
                swap(nums, j, j + 1);
            }
        }
    }

    public static void bubbleSort(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        int N = nums.length;
        for (int e = N - 1; e > 0; e--) {
            for (int i = 0; i < e; i++) {
                if (nums[i] > nums[i + 1]) {
                    swap(nums, i, i + 1);
                }
            }
        }
    }

    public static void selectSort(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }
        int N = nums.length;
        for (int i = 0; i < N - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < N; j++) {
                minIndex = nums[j] < nums[minIndex] ? j : minIndex;
            }
            if (minIndex != i) {
                swap(nums, i, minIndex);
            }
        }
    }

    public static void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}
